package com.yang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private EditText phoneText;
    private EditText nameText;
    private TextView showInfoView;
    private Button addBtn;
    private Button queryBtn;
    private Button deleteBtn;
    private Button updateBtn;
    private Button queryByNameBtn;

    private Button dialBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneText = findViewById(R.id.phoneText);
        nameText = findViewById(R.id.nameText);
        addBtn = findViewById(R.id.addBtn);
        queryBtn = findViewById(R.id.queryBtn);
        deleteBtn = findViewById(R.id.deleteBtn);
        updateBtn = findViewById(R.id.updateBtn);
        queryByNameBtn = findViewById(R.id.queryByNameBtn);
        showInfoView = findViewById(R.id.showInfoView);
        dialBtn = findViewById(R.id.dialBtn);

        ContactHelper contactHelper = new ContactHelper(MainActivity.this);
        addBtn.setOnClickListener(view -> {
            String name = nameText.getText().toString();
            String phone = phoneText.getText().toString();
            Boolean res = contactHelper.insert(name, phone);
            if (res) {
                nameText.setText("");
                phoneText.setText("");
            }
        });

        queryBtn.setOnClickListener(view -> {
//            String res = contactHelper.queryAll();
//            if (res != null) {
//                showInfoView.setText(res);
//            }
            Intent intent = new Intent(MainActivity.this, ShowActivity.class);
            startActivity(intent);
        });

        deleteBtn.setOnClickListener(view -> {
            int res = contactHelper.deleteByName(nameText.getText().toString());
            if (res > 0) {
                nameText.setText("");
                phoneText.setText("");
            }
//            contactHelper.deleteById(Integer.valueOf(nameText.getText().toString()));
        });

        queryByNameBtn.setOnClickListener(view -> {
            String name = nameText.getText().toString();
            String phone = contactHelper.queryPhoneByName(name);
            if (phone != null) {
                phoneText.setText(phone);
            }
        });

        updateBtn.setOnClickListener(view -> {
            String name = nameText.getText().toString();
            String phone = phoneText.getText().toString();
            int res = contactHelper.update(name, phone);
            if (res > 0) {
                phoneText.setText("");
            }
        });

        dialBtn.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.DIAL");
            intent.setData(Uri.parse("tel:" + phoneText.getText().toString()));
            startActivity(intent);
        });
    }
}